<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/controller/ctlAbstract.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/user.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busUser.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busItem.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busProvider.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busAvatar.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busResource.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/IPInformation.php';

class HomeController extends AbstractController {
	private $_userBus;
	private $_itemBus;
	private $_providerBus;
	private $_avatarBus;
	private $_resourceBus;
	
	public function main(array $getVars) {
		global $ubi;
		
		$this->_userBus = new UserBus();
		$this->_itemBus = new ItemBus();
		$this->_providerBus = new ProviderBus();
		$this->_avatarBus = new AvatarBus();
		$this->_resourceBus = new ResourceBus();
		
		if (isset($_COOKIE[$ubi['SITE']['COOKIE_USERNAME']]) && isset($_COOKIE[$ubi['SITE']['COOKIE_PASSWORD']])) {
			$username = $_COOKIE[isset($_COOKIE[$ubi['SITE']['COOKIE_USERNAME']])];
			$password = $_COOKIE[isset($_COOKIE[$ubi['SITE']['COOKIE_USERNAME']])];
			$user = $this->_userBus->login($username, $password);
			
			if ($user) {
				//user has logged in successfully				
				//create session
				$_SESSION['userid'] = $user->getUserID();
				$_SESSION['username'] = $user->getUserName();
				$_SESSION['email'] = $user->getEmail();
			}
		} else {		
			if (isset($_SESSION['userid'])) {			
				$user = $this->_userBus->getUser($_SESSION['userid']);
			}		
		}			
		
		if (isset($getVars['view']) && $getVars['view'] == 'map') {
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewHome_Map.php';
		} else {
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewHome.php';
		}		
	}
}
?>